<template>
  <div class="login_container" v-loading.fullscreen.lock="fullscreenLoading">
    <div class="login_box">
      <div class="avatar_box">
        <!-- 登陆头像 -->
        <img src="../assets/logo.png" alt="" />
      </div>
      <div class="login_form">
        <!-- 登录表单 -->
        <el-form
          ref="loginFormRefs"
          label-width="80px"
          :model="loginForm"
          :rules="loginRules"
          :hide-required-asterisk="true"
        >
          <!-- 用户名 -->
          <el-form-item label="账号" prop="username">
            <el-input
              prefix-icon="el-icon-user"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item label="密码" prop="password">
            <el-input
              prefix-icon="el-icon-lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
        </el-form>
        <!-- 登陆表单结束 -->
        <!-- 登录按钮 -->
        <div class="formButton">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fullscreenLoading: false,
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 7,
            message: "长度在 3 到 7 个字符",
            trigger: "change",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    loading: function () {
      
    },
    resetForm: function () {
      this.$refs.loginFormRefs.resetFields();
    },
    login: function () {
      this.$refs.loginFormRefs.validate(async (isPassed) => {
        if (!isPassed) {
          this.$alert("用户名或密码格式错误", "验证未通过", {
            confirmButtonText: "确定",
          });
          return;
        } else {
          this.fullscreenLoading = true
          const { data: res } = await this.$http.post("login", this.loginForm);
          
          if (res.meta.status != 200) {
            // 弹框
            this.$alert("用户名或密码错误", "登陆失败", {
              confirmButtonText: "确定",
            });
            this.resetForm();
            this.fullscreenLoading = false
            return console.log("登录失败");
          } else {
            this.$message({
              message: "登陆成功",
              type: "success",
            });
            console.log("登录成功");
            // 对token的有关操作
            window.sessionStorage.setItem("token", res.data.token);
            this.fullscreenLoading = 'false'
            this.$router.push("/home");
          }
          
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background-image: linear-gradient(
    120deg,
    #55efc4,
    #6c5ce7,
    #fdcb6e,
    #00cec9,
    #b2bec3
  );
  background-size: 400%;
  animation: move 15s infinite;
}
@keyframes move {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 15px #aaaaaa;
  .login_form {
    width: 400px;
    //   margin: 0 0 0 25px;
    padding-top: 105px;
    .formButton {
      position: absolute;
      right: 50px;
      margin-top: 5px;
    }
  }
  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
</style>